package com.topway.dsmflex.ui.composite.synchro;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.Application;
import com.Consts;
import com.swtdesigner.SWTResourceManager;
import com.topway.dsmflex.Alert;
import com.topway.dsmflex.Log;
import com.topway.dsmflex.model.base.DataMonitorBean;
import com.topway.dsmflex.model.synchro.DataColumnBean;
import com.topway.dsmflex.model.synchro.DataSourceBean;
import com.topway.dsmflex.model.synchro.DataSqlBean;
import com.topway.dsmflex.model.synchro.DataTableBean;
import com.topway.dsmflex.services.synchro.SqlServices;
import com.topway.dsmflex.thread.job.SynchroMonitor;
import com.topway.dsmflex.ui.InitFormComposite;
import com.topway.dsmflex.ui.contentProvider.TableContentProvider;
import com.topway.dsmflex.ui.dialog.ExpressionDialog;
import com.topway.dsmflex.ui.dialog.SqlDialog;
import com.topway.dsmflex.ui.labelProvider.SqlNameProvider;



/**
 * Sql同步
 * 
 * @author liusy 2013-05-8
 *
 */
public class SqlSynchro extends InitFormComposite {
	
   public static String name = "同步sql设置";
   
   @SuppressWarnings("unused")
private static Logger log = Logger.getLogger(SqlSynchro.class);
   
   private Shell shell;
   private Table         tblTab;
   private CheckboxTableViewer   tvTab;
   private TableColumn   sourceTable;
   private Button btnStart;
   private TableColumn   colOperator;
   private TableColumn colSourceOperator;
   private ComboBoxCellEditor comTargetTableName;
   private ComboBoxCellEditor comTargetDs;
   
   private DataSourceBean dsSource;
   private DataSqlBean  dSql;
   private SynchroMonitor synchroMonitor = null;
   private SqlServices sqlServices = null;

   private String [] targetTables  = new String[]{};
   private String [] targetColumns  = new String[]{};
   private String [] targetSource = new String[]{};
   
   
   
   public List<DataSqlBean> dSqlList=null;
   
   public SqlSynchro(Composite parent, int style) {
      super(parent, style);
      createContents();
      initForm();
   }
   
	   
	   
   public int initForm() {
	   this.synchroMonitor = SynchroMonitor.getSynchroMonitor();
	   this.sqlServices = SqlServices.getSqlServices();
		dSqlList = sqlServices.getAllDataTable(dsSource);
		if(null !=dSqlList && dSqlList.size()>0)
			tvTab.setInput(dSqlList);
		for (int i = 0; i < dSqlList.size(); i++) {
				dSqlList.get(i).addPropertyChangeListener(this);
			if (dSqlList.get(i).getCheck()== 1)
			{
				tvTab.setChecked(dSqlList.get(i), true);
			}
		}
		
	   return 1;
   }

   protected void createContents() {
	   
	  shell = this.getShell();
                                                setLayout(new FormLayout());
                                                Button btnSvae = new Button(this, SWT.NONE);
                                                FormData fd_btnSvae = new FormData();
                                                fd_btnSvae.bottom = new FormAttachment(100,-5);
                                                fd_btnSvae.left = new FormAttachment(50,-30);
                                                btnSvae.setSize(30, 50);
                                                btnSvae.setLayoutData(fd_btnSvae);
                                                //   btnSvae.setAlignment(SWT.LEFT);
                                                   btnSvae.addSelectionListener(new SelectionAdapter() {
                                                      public void widgetSelected(final SelectionEvent e) {
                                                     	 Object [] obj = (Object[]) tvTab.getCheckedElements();
                                                     	 List<DataSqlBean> list = new ArrayList<DataSqlBean>();
                                                     	 for (int i = 0; i < obj.length; i++) {
																list.add((DataSqlBean) obj[i]);
															}
                                                     	 sqlServices.saveTableConfigList(list);
                                                     	 if(list.size()>0)
                                                     	 {
                                                     	 	Log.clearLog();
                                                     	 	//Alert.MsgBox("提示", "保存成功");
                                                     	 	Application.openMenu(SystemLog.name);
                                                     	 }
                                                      }
                                                   });
                                                   
                                                   btnSvae.setImage(SWTResourceManager.getImage(Application.class, "/img/savesetting.gif"));
                                                   btnSvae.setFont(SWTResourceManager.getFont("", 10, SWT.BOLD));
                                                   btnSvae.setText("编译");
                                                   btnSvae.setToolTipText("编译并保存，如未通过，日志将以红色字体提示");
                                                         
                                                         
                                                         tvTab = CheckboxTableViewer.newCheckList(this, SWT.BORDER | SWT.FULL_SELECTION);
                                                         tvTab.setContentProvider(new TableContentProvider());
                                                         tvTab.setLabelProvider(new SqlNameProvider());
                                                         tvTab.setColumnProperties(TABLE_INPUTMETAS);
                                                         tblTab = tvTab.getTable();
                                                         FormData fd_tblTab = new FormData();
                                                         fd_tblTab.bottom = new FormAttachment(93, 0);
                                                         fd_tblTab.top = new FormAttachment(0,0);
                                                         fd_tblTab.left = new FormAttachment(0,0);
                                                         fd_tblTab.right = new FormAttachment(100,0);
                                                         tblTab.setLayoutData(fd_tblTab);
                                                         tblTab.setLinesVisible(true);
                                                         tblTab.setHeaderVisible(true);
//                                                         CellEditor[] cellEditor = new CellEditor[11];
//                                                         cellEditor[0] = new CheckboxCellEditor(tblTab, SWT.NONE);
//                                                         cellEditor[2] = new SqlCellEditor(tblTab,"TableNameSource");
//                                                         cellEditor[3] = new ComboBoxCellEditor(tblTab, Consts.TABLE_OPERATOR_LABEL, SWT.READ_ONLY);
//                                                         cellEditor[4]= new SourceUpdateSqlCellEditor(tblTab);
//                                                         comTargetTableName= new ComboBoxCellEditor(tblTab, targetTables, SWT.READ_ONLY);
//                                                         cellEditor[5] = comTargetTableName;
//                                                         comTargetDs = new ComboBoxCellEditor(tblTab, targetSource, SWT.READ_ONLY);
//                                                         cellEditor[6] =comTargetDs;
//                                                         cellEditor[7]= new TargetExecuteSqlCellEditor(tblTab);
//                                                         cellEditor[8]= new CurrentSourceSqlCellEditor(tblTab);
//                                                         cellEditor[9]= new CurrentTargetSqlCellEditor(tblTab);
//                                                         cellEditor[10] =  new TextCellEditor(tblTab, SWT.NONE);
//                                                         
//
//                                                         tvTab.setCellEditors(cellEditor);
//                                                         tvTab.setCellModifier(new TabInputsCellModifier(tvTab));
                                                         
                                                         TableColumn   colTabCheck = new TableColumn(tblTab, SWT.CHECK);
                                                         colTabCheck.setWidth(23);
                                                         
                                                         
                                                         TableColumn state = new TableColumn(tblTab, SWT.NONE);
                                                         state.setWidth(15);
                                                         state.setAlignment(SWT.CENTER);
                                                         state.setText("  ");
                                                         
                                                         TableColumn size = new TableColumn(tblTab, SWT.NONE);
                                                         size.setWidth(50);
                                                         size.setAlignment(SWT.CENTER);
                                                         size.setText("数量");
                                                         
                                                         
                                                         TableColumn name = new TableColumn(tblTab, SWT.NONE);
                                                         name.setWidth(150);
                                                         state.setAlignment(SWT.CENTER);
                                                         name.setText("名称");
                                                         
                                                         TableColumn statu = new TableColumn(tblTab, SWT.NONE);
                                                         statu.setWidth(50);
                                                         statu.setAlignment(SWT.CENTER);
                                                         statu.setText("激活");
                                                         
                                                         TableColumn sourceSource = new TableColumn(tblTab, SWT.NONE);
                                                         sourceSource.setWidth(70);
                                                         sourceSource.setAlignment(SWT.CENTER);
                                                         sourceSource.setText("源数据源");
                                                         
                                                         sourceTable = new TableColumn(tblTab, SWT.NONE);
                                                         sourceTable.setWidth(125);
                                                         sourceTable.setText("源查询语句");
                                                         
                                                         colOperator = new TableColumn(tblTab, SWT.NONE);
                                                         colOperator.setWidth(80);
                                                         colOperator.setText("目标操作");
                                                         
                                                         TableColumn targetSource_1 = new TableColumn(tblTab, SWT.NONE);
                                                         targetSource_1.setWidth(80);
                                                         targetSource_1.setText("目标数据源");
                                                 
                                                         
                                                              TableColumn targetTable = new TableColumn(tblTab, SWT.NONE);
                                                              targetTable.setWidth(125);
                                                              targetTable.setText("目标表");
                                                              
                                                              TableColumn synchroNterval = new TableColumn(tblTab, SWT.NONE);
                                                              synchroNterval.setWidth(70);
                                                              synchroNterval.setText("频率(分)");
                                                              
                                                              
                                                              colSourceOperator = new TableColumn(tblTab, SWT.NONE);
                                                              colSourceOperator.setWidth(80);
                                                              colSourceOperator.setText("源后行");
                                                 
                                                              
                                                              TableColumn  targetExecuteSql = new TableColumn(tblTab, SWT.NONE);
                                                              targetExecuteSql.setWidth(80);
                                                              targetExecuteSql.setText("目标后行");
                                                              
                                                              TableColumn currentSourceSql = new TableColumn(tblTab, SWT.NONE);
                                                              currentSourceSql.setWidth(80);
                                                              currentSourceSql.setText("源并行");
                                                              
                                                              TableColumn currentTargetSql = new TableColumn(tblTab, SWT.NONE);
                                                              currentTargetSql.setWidth(80);
                                                              currentTargetSql.setText("目标并行");
                                                             
    
                                                                    // 添加托盘右键菜单
                                                               final Menu menu = new Menu(shell, SWT.POP_UP);
                                                               final MenuItem menuAdd = new MenuItem(menu, SWT.PUSH);//开始 
                                                               menuAdd.setText("添加");
                                                               menuAdd.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
			AddSqlSynchro addSqlSynchro = new AddSqlSynchro(shell.getShell(), SWT.NONE, null);
			dSql = (DataSqlBean) addSqlSynchro.open();
			if(null==dSql)
				return;
			tvTab.add(dSql);
			tvTab.setChecked(dSql, true);
		}
	});
                                                               final MenuItem menuUpdate = new MenuItem(menu, SWT.PUSH);//开始 
                                                               menuUpdate.setText("修改");
                                                               menuUpdate.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
 			IStructuredSelection obj = (IStructuredSelection)tvTab.getSelection();
			   dSql = (DataSqlBean) obj.getFirstElement();
			   if(null==dSql)
			   return;
			   AddSqlSynchro addSqlSynchro = new AddSqlSynchro(shell.getShell(), SWT.NONE, dSql);
			   DataSqlBean _dSql = (DataSqlBean) addSqlSynchro.open();
			   if(null!=_dSql)
			   {
//				   tvTab.remove(dSql);
//				   dSql=_dSql;
//				   tvTab.add(dSql);
//				   tvTab
//				   tvTab.setChecked(dSql, true);
				   tvTab.refresh();
			   }
		
		}
	});
                                                               final MenuItem menuDelete = new MenuItem(menu, SWT.PUSH);
                                                               menuDelete.setText("删除");
                                                               menuDelete.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
 			IStructuredSelection obj = (IStructuredSelection)tvTab.getSelection();
			   dSql = (DataSqlBean) obj.getFirstElement();
			   if(null==dSql)
				   return;
			   tvTab.remove(dSql);
		}
	});
                                                               tblTab.setMenu(menu);
            
            tvTab.addDoubleClickListener(new IDoubleClickListener() {
		@Override
		public void doubleClick(DoubleClickEvent arg0) {
			IStructuredSelection obj = (IStructuredSelection)arg0.getSelection();
			   dSql = (DataSqlBean) obj.getFirstElement();
				AddSqlSynchro addSqlSynchro = new AddSqlSynchro(shell.getShell(), SWT.NONE, dSql);
				   DataSqlBean _dSql = (DataSqlBean) addSqlSynchro.open();
				   if(null!=_dSql)
				   {
					   tvTab.remove(dSql);
					   dSql=_dSql;
					   tvTab.add(dSql);
					   tvTab.setChecked(dSql, true);
				   }
		}
	});
   }

	private DataSqlBean createDataSqlBean(String selectSourceSql)
	{
		if(null==dsSource)
			return null;
		DataSqlBean _dSql = new DataSqlBean(selectSourceSql);
		_dSql.setTargetDataSource(dsSource);
		_dSql.setSourceDataSource(dsSource);
		_dSql.setDsSource(dsSource.getDataSourceName());
		_dSql.setDsTarget(dsSource.getDataSourceName());
		return _dSql;
	}
   
   class InputMetaLabelProvider extends LabelProvider implements ITableLabelProvider {
	      public String getColumnText(Object obj, int columnIndex) {
	    	  DataColumnBean dsi = (DataColumnBean )  obj;
	         switch (columnIndex) {
	         case 0:
	            return dsi.getColumnNameSource();
	         case 1:
	        	 return dsi.getIsKey();
	         case 2:
	        	 return dsi.getColumnNameTarget();
	         case 3:
	        	 return dsi.getColExpress();
	         }
	         return null;
	      }

	      private String getLabel(String[] labels, String[] values, String name) {
	         for (int i = 0; i < values.length; i++) {
	            if (values[i].equalsIgnoreCase(name)) return labels[i];
	         }
	         return "";
	      }

	      public String getText(Object element) {
	         return element == null ? "" : element.toString();
	      }

	      public Image getColumnImage(Object obj, int index) {
	         return null;
	      }
	   }

	   public static String[] INPUTMETAS = new String[] { "columnNameSource","isKey" ,"columnNameTarget","colExpress"};
	   public static String[] TABLE_INPUTMETAS = new String[] { "check","sourceSource","selectSourceSql", "operatorTarget", "updateSourceSql","tableNameTarget","targetSource","targetExecuteSql","currentSourceSql","currentTargetSql","synchroNterval"};


	   class InputsCellModifier implements ICellModifier {
	      private TableViewer tv;

	      public InputsCellModifier(TableViewer tv) {
	         this.tv = tv;
	      }

	      public boolean canModify(Object element, String property) {
	         return true;
	      }

	      public Object getValue(Object element, String property) {
	    	  DataColumnBean o = (DataColumnBean ) element;
	        if (property.equals("isKey")) return getIndex(Consts.TABLE_KEY, o.getIsKey());
	        else if (property.equals("columnNameSource")) return o.getColumnNameSource();
	        else if (property.equals("columnNameTarget"))  return getIndex(targetColumns, o.getColumnNameTarget()); 
	        else if (property.equals("colExpress")) return (null==o.getColExpress()?"":o.getColExpress());
	        throw new RuntimeException("错误的列别名:" + property);
	      }

	      private int getIndex(String[] labels, String name) {
	         for (int i = 0; i < labels.length; i++) {
	            if (labels[i].equalsIgnoreCase(name)) return i;
	         }
	         return 0;
	      }

	      public void modify(Object element, String property, Object value) {
	         TableItem item = (TableItem) element;
	         DataColumnBean o = (DataColumnBean ) item.getData();
	          if (property.equals("isKey")) {
		            o.setIsKey(Consts.TABLE_KEY[(Integer) value]);
		         }
	         else  if (property.equals("columnNameSource")) {
		            o.setColumnNameSource((String) value);
			     }
	         else  if (property.equals("columnNameTarget")) {
		        		 o.setColumnNameTarget(targetColumns[(Integer) value]);
	        	}
	         else  if (property.equals("colExpress")) {
		            o.setColExpress((String) value);
			     }
	         else {
	             throw new RuntimeException("错误的列别名:" + property);
	          }
	         tv.update(o, null);
	      }
	   }
	   class TabInputsCellModifier implements ICellModifier {
		      private CheckboxTableViewer tv;

		      public TabInputsCellModifier(CheckboxTableViewer tv) {
		         this.tv = tv;
		      }

		      public boolean canModify(Object element, String property) {
		         return true;
		      }

		      public Object getValue(Object element, String property) {
		    	  DataTableBean o = (DataTableBean ) element;
		    	     
			         
		        if (property.equals("operatorTarget")) return getIndex(Consts.TABLE_OPERATOR, o.getOperatorTarget());
		        else if(property.equals("updateSourceSql"))return o.getUpdateSourceSql();
		        else if(property.equals("targetSource"))return getIndex(targetSource, o.getDsTarget());
		        else if(property.equals("selectSourceSql"))return o.getSelectSourceSql();
		        else if(property.equals("tableNameTarget")) return getIndex(targetTables, o.getTableNameTarget());
		        else if(property.equals("targetExecuteSql")) return o.getTargetExecuteSql();
		        else if(property.equals("currentSourceSql")) return o.getCurrentSourceSql();
		        else if(property.equals("currentTargetSql")) return o.getCurrentTargetSql();
		        else if(property.equals("synchroNterval")) return  String.valueOf(o.getSynchroNterval());
		        throw new RuntimeException("错误的列别名:" + property);
		      }

		      private int getIndex(String[] labels, String name) {
		         for (int i = 0; i < labels.length; i++) {
		            if (labels[i].equalsIgnoreCase(name)) return i;
		         }
		         return 0;
		      }
		      
		      public void modify(Object element, String property, Object value) {
		         TableItem item = (TableItem) element;
		         DataTableBean o = (DataTableBean ) item.getData();
		         if (property.equals("operatorTarget")) {
			            o.setOperatorTarget(Consts.TABLE_OPERATOR[(Integer) value]);
			         } else if(property.equals("updateSourceSql")){
			        	 o.setUpdateSourceSql((String)value);
			         } else if(property.equals("selectSourceSql")){
			        	 o.setSelectSourceSql((String) value);
			        	 o.setTableNameSource((String) value);
			         }else if(property.equals("targetExecuteSql")){
			        	 o.setTargetExecuteSql((String) value);
			         }else if(property.equals("currentSourceSql")){
			        	 o.setCurrentSourceSql((String) value);
			         }else if(property.equals("currentTargetSql")){
			        	 o.setCurrentTargetSql((String) value);
			         }else if(property.equals("synchroNterval")){
			        	 float v=o.getSynchroNterval();
			        	 try {
			        		 v=Float.parseFloat((String) value);
						} catch (Exception e) {
							Alert.MsgBox("错误", (String) value+"错误！！");
						}
			        	 o.setSynchroNterval(v);
			         } else if(property.equals("tableNameTarget")){
			        	 try {
			        		 o.setTableNameTarget(targetTables[(Integer) value]);
						} catch (ArrayIndexOutOfBoundsException e) {
						}
			         }else if(property.equals("targetSource")){
			        	 o.setDsTarget(targetSource[(Integer) value]);
			 			IStructuredSelection obj = (IStructuredSelection)tvTab.getSelection();
						   dSql = (DataSqlBean) obj.getFirstElement();
						targetTables =  sqlServices.getTargetTablesByDb(dSql);
					    	  comTargetTableName.setItems(targetTables);
			         }
			         else {
			             throw new RuntimeException("错误的列别名:" + property);
			          }
		         tv.update(o, null);
		      }
		   }
	   
	   
	   class SqlCellEditor extends DialogCellEditor {

		      private Table table;
		      private String column;
		      public SqlCellEditor(Composite parent,String column) {
		         super(parent);
		         this.table = (Table) parent;
		         this.column= column;
		      }
		      protected Object openDialogBox(Control cellEditorWindow) {
		         TableItem[] selectItems = table.getSelection();
		         DataSqlBean dataSql = null;
		         String title = "";
		         String value = "";
		         if (selectItems != null && selectItems.length > 0)
		        	 dataSql = (DataSqlBean) selectItems[0].getData();
		         if(null!=dataSql)
		         {
		        	 if(column.equals("TableNameSource"))
		        	 {
		        		 title="查询";
		        		 value = null == dataSql.getSelectSourceSql()?"":dataSql.getSelectSourceSql();
		        	 }
		        	 SqlDialog gcd = new SqlDialog(cellEditorWindow.getShell(),title, value);
		        	 String sql = gcd.open();
		         	 if(column.equals("TableNameSource"))
		        	 {
		         		 dataSql.setTableNameSource(sql);
		        	 }
		        	 return sql;
		         }
		        return "";
		      }
		   }
	   class SourceUpdateSqlCellEditor extends DialogCellEditor {

		      private Table table;

		      public SourceUpdateSqlCellEditor(Composite parent) {
		         super(parent);
		         this.table = (Table) parent;
		      }
		      protected Object openDialogBox(Control cellEditorWindow) {
		         TableItem[] selectItems = table.getSelection();
		         DataSqlBean dataSql = null;
		         if (selectItems != null && selectItems.length > 0) dataSql = (DataSqlBean) selectItems[0].getData();
		         SqlDialog gcd = new SqlDialog(cellEditorWindow.getShell(),"更新、删除", null==dataSql.getUpdateSourceSql()?"":dataSql.getUpdateSourceSql());
		         String sql = gcd.open();
		         return sql;
		      }
		   }
	   class ExpressionCellEditor extends DialogCellEditor {

		      private Table table;

		      public ExpressionCellEditor(Composite parent) {
		         super(parent);
		         this.table = (Table) parent;
		      }
		      protected Object openDialogBox(Control cellEditorWindow) {
		         TableItem[] selectItems = table.getSelection();
		         String exp = "";
		         if (selectItems != null && selectItems.length > 0) exp = ((DataColumnBean) selectItems[0].getData()).getColExpress();
		         ExpressionDialog gcd = new ExpressionDialog(cellEditorWindow.getShell(), null==exp?"":exp);
		         String res = gcd.open();
		         return res;
		      }
		   }
	   
		   class TargetExecuteSqlCellEditor extends DialogCellEditor {

			      private Table table;

			      public TargetExecuteSqlCellEditor(Composite parent) {
			         super(parent);
			         this.table = (Table) parent;
			      }
			      protected Object openDialogBox(Control cellEditorWindow) {
			         TableItem[] selectItems = table.getSelection();
			         DataSqlBean dataSql = null;
			         if (selectItems != null && selectItems.length > 0) dataSql = (DataSqlBean) selectItems[0].getData();
			         SqlDialog gcd = new SqlDialog(cellEditorWindow.getShell(),"操作目标表后执行", null == dataSql.getTargetExecuteSql()?"":dataSql.getTargetExecuteSql());
			         String sql = gcd.open();
			         dataSql.setTargetExecuteSql(sql);
			         return sql;
			      }
			   }
		   
		   class CurrentTargetSqlCellEditor extends DialogCellEditor {

			      private Table table;

			      public CurrentTargetSqlCellEditor(Composite parent) {
			         super(parent);
			         this.table = (Table) parent;
			      }
			      protected Object openDialogBox(Control cellEditorWindow) {
			         TableItem[] selectItems = table.getSelection();
			         DataSqlBean dataSql = null;
			         if (selectItems != null && selectItems.length > 0) dataSql = (DataSqlBean) selectItems[0].getData();
			         SqlDialog gcd = new SqlDialog(cellEditorWindow.getShell(),"同步数据同时执行目标库，使用{列}，当变量，类型为String", null == dataSql.getCurrentTargetSql()?"":dataSql.getCurrentTargetSql());
			         String sql = gcd.open();
			         dataSql.setCurrentTargetSql(sql);
			         return sql;
			      }
			   }
		   
		   class CurrentSourceSqlCellEditor extends DialogCellEditor {

			      private Table table;

			      public CurrentSourceSqlCellEditor(Composite parent) {
			         super(parent);
			         this.table = (Table) parent;
			      }
			      protected Object openDialogBox(Control cellEditorWindow) {
			         TableItem[] selectItems = table.getSelection();
			         DataSqlBean dataSql = null;
			         if (selectItems != null && selectItems.length > 0) dataSql = (DataSqlBean) selectItems[0].getData();
			         SqlDialog gcd = new SqlDialog(cellEditorWindow.getShell(),"同步数据同时执行目源库， 使用{列}，当变量，类型为String", null == dataSql.getCurrentSourceSql()?"":dataSql.getCurrentSourceSql());
			         String sql = gcd.open();
			         dataSql.setCurrentSourceSql(sql);
			         return sql;
			      }
			   }
		   
		   
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if(DataMonitorBean.SIZE.equals(event.getPropertyName()))
					((DataSqlBean)event.getSource()).setSize(Integer.parseInt(event.getNewValue().toString()));	
				if(DataMonitorBean.STATE.equals(event.getPropertyName()))
		 			((DataSqlBean)event.getSource()).setState(Integer.parseInt(event.getNewValue().toString()));	
				tvTab.refresh();
//				System.out.println(event.getNewValue());
//				System.out.println(event.getOldValue());
			}
}
